/**
 * 用户认证模块接口列表
 */
import base from './base'; // 导入接口域名列表
import http from '../utils/http'; // 导入http中创建的axios实例
import store from '../store';
//import qs from 'qs'; // 根据需求是否导入qs模块
import { message } from 'ant-design-vue'

import router from '../router';
import md5 from 'js-md5';

var Message = message



const auth = {
    login(params) {
        let formData = new FormData
        formData.append("password", md5(params.password));
        http.post(`${base.url.auth}/login`, formData)
            .then(res => {
                if (res.data) {
                    store.commit('SET_TOKEN', { str: res.data.result.token.str, exp: res.data.result.token.exp })
                    Message.info('登录成功！');                    
                    router.replace({
                        path: params.redirect
                    });
                    const token = store.state.common.token.str;//localStorage.token
                    console.log(token);
                } else {
                    Message.error('登录失败');
                }
            })
            .catch(err => {
                console.log(err)
                Message.error('error: ' + err.data.message);
            });
    },
    register(params) {
        let formData = new FormData
        formData.append("password", md5(params.password1));
        formData.append("password2", md5(params.password2));
        http.post(`${base.url.auth}/register`, formData)
            .then(res => {
                if (res.data.result) {
                    store.commit('SET_TOKEN', { str: res.data.result.token.str, exp: res.data.result.token.exp })
                    //console.log(res.data)
                    Message.info('欢迎您，激活成功!');
                    router.replace({
                        path: "/"
                    });
                } else {
                    //Message.error('注册失败');
                }
            });
    },
    update(params) {
        let formData = new FormData
        formData.append("password", md5(params.password1));
        formData.append("password2", md5(params.password2));
        http.post(`${base.url.auth}/auth/update`, formData)
            .then(res => {
                if (res.data) {
                    //store.commit('SET_TOKEN', { str: res.data.result.token.str})
                    console.log(res.data)
                    Message.info('修改成功！');
                } else {
                    Message.error('修改失败');
                    //this.forgetPassword = true;
                }
            })
            .catch(err => {
                Message.error('修改出错：' + err.data.message);
            });
    },
    autoLogin() {
        router.replace({
            path: '/auth/login',
            query: {
                redirect: router.currentRoute.fullPath
            }
        });
    },
    logout() {
        let _that = this
        http.get(`${base.url.auth}/logout`)
            .then(res => {
                if (res.data) {
                    store.commit('SET_TOKEN', {})
                    _that.autoLogin()
                    Message.info('已经注销登录');
                } else {
                    Message.error('注销失败');
                }
            })
            .catch(err => {
                Message.error('注销出错：' + err);
            });
    },
    async getUserInfo() {
        try {
            const res = await http.get(`${base.url.auth}/auth/info`);
            if (res.data.result) {
                return res.data.result;
            }
        }
        catch (err) {            
            return Promise.reject(err);
        }
    },
    async checkFirst() {
        try {
            const res = await http.get(`${base.url.auth}/first`);
            if (res.data.result) {
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    }
}

export default auth;
